﻿@using System.Collections
@{
    ViewBag.Title = "Đại lý - Quản lý đơn hàng";
    var listMonths = (Dictionary<int, string>)ViewData["listMonths"];
    var listStatus = (Dictionary<int, string>)ViewData["listStatus"];
    var resellers = (List<SuperStar.Models.Reseller>)ViewData["resellers"];
}

<div class="box">
    <h2 class="head-big-title">
        <a href="#"><span>Đại lý - Quản lý đơn hàng</span></a>
    </h2>
    <div class="sub_content">
        <br/>
        @using (Html.BeginForm("Index", "Reseller", FormMethod.Post))
        {
            if (Roles.IsUserInRole("Administrator"))
            {
                <span>Đại lý:</span>
                 <select name="reseller" onchange=" this.form.submit() ">
                    @foreach (var reseller in resellers)
                    {
                        <option @(Request.Params["reseller"] == reseller.Id.ToString() ? "selected" : "")
                            value="@reseller.Id">@reseller.Name</option>
                    }
                          
                </select>
            }
            
            <span>Thời gian nhận:</span> <select name="month" onchange=" this.form.submit() ">
                                     @foreach (var item in listMonths)
                                     {
                                         <option @(Request.Params["month"] == item.Key.ToString() ? "selected" : "")
                                             value="@item.Key">@item.Value</option>
                                     }
                          
                                 </select>
             <span> Tình trạng :</span> <select name="status" onchange=" this.form.submit() ">
                                 @foreach (var item in listStatus)
                                 {
                                     <option @(Request.Params["status"] == item.Key.ToString() ? "selected" : "")
                                         value="@item.Key">@item.Value</option>
                                 }
                             </select>
        }
        <br/>
        <div id="BillTable" style="width: 100%; margin: auto;"></div>
    </div>
</div>
<script type="text/javascript">

    $(document).ready(function() {
        //Prepare jtable plugin
        $('#BillTable').jtable({
            paging: true, //Enable paging
            pageSize: 10, //Set page size (default: 2)
            //sorting: true, //Enable sorting
            //defaultSorting: 'Name Id', //Set default sorting
            actions: {
                listAction: '/Admincp/Reseller/GetLists?reseller=@ViewData["reseller"]&month=@ViewData["month"]&status=@ViewData["status"]',
                updateAction: '/Admincp/Reseller/Update',
            },
            fields: {
                Id: {
                    title: "Hóa đơn",
                    key: true,
                    create: false,
                    edit: true,
                    width: '8%'
                },
                PromotionDetail: {
                    title: 'Khuyến mãi',
                    width: '8%',       
                    sorting: false,
                    edit: false,
                    create: false,
                    display: function(bill) {
                        //Create an image that will be used to open child table
                        var $img = $('<img src="/Content/images/note.png" title="Xem danh sách khuyến mãi hóa đơn #'+bill.record.Id+'" />');
                        //Open child table when user clicks the image
                        $img.click(function() {
                            $('#BillTable').jtable('openChildTable',
                                $img.closest('tr'),
                                {
                                    title: 'Sản phẩm khuyến mãi hóa đơn #'+bill.record.Id,
                                    actions: {
                                        listAction: '/Admincp/Reseller/GetListDetailBill?billId=' + bill.record.Id
                                    },
                                    fields: {
                                        Id: {
                                            title: 'Mã SPKM',
                                            key: true,
                                            create: false,
                                            edit: false,
                                        },
                                        Name: {
                                            title: 'Tên SP Khuyến mãi',
                                            width: '30%'
                                        },
                                        Quantity: {
                                            title: 'Số lượng',
                                        }
                                    }
                                }, function(data) { //opened handler
                                    data.childTable.jtable('load');
                                });
                        });
                        //Return image to show on the person row
                        return $img;
                    }
                },
                
                Customer_Id: {
                    title: 'Khách hàng',
                    list: false,
                    edit: false
                },
                Customer_Name: {
                    title: 'Khách hàng',
                    type: 'readonly',
                },
                Status_Id: {
                   title: 'Trạng thái đơn hàng',
                   options: '/Admincp/Reseller/GetStatus',
                   list:false,
                },
                Product_Id: {
                    title: 'Mã SP mua',
                    type: 'readonly',
                    width: '8%'
                },
                Product_Name: {
                    title: 'Tên SP mua',
                    type: 'readonly',
                    width: '20%'
                },
                Price: {
                    title: 'Tổng thanh toán',
                    type: 'readonly',
                    display: function(data) {
                        return accounting.formatNumber(data.record.Price, 0, ",") + '<sup>đ</sup>';
                    }
                },
                Quantity: {
                    title: 'SL mua',
                    type: 'readonly',
                    width: '8%'
                },
                DateAdded: {
                    title: 'Đặt hàng ngày',
                    type: 'date',
                    displayFormat: 'dd-mm-yy',
                    create: false,
                    edit: false
                },
                ReceivedDate: {
                    title: 'Đã nhận ngày',
                    type: 'date',
                    displayFormat: 'dd-mm-yy',
                    create: false,
                    edit: false
                }
            },
            formCreated: function(event, data) {

            },
            formSubmitting: function(event, data) {
                
            },
            formClosed: function(event, data) {
                  $('#BillTable').jtable('load');
            }
        });

        //Load ProductTable list from server
        $('#BillTable').jtable('load');

    });

</script>